//
//  GBModifyPassWordViewController.m
//  警用舆情平台
//
//  Created by 郭斌 on 15/6/15.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "GBModifyPassWordViewController.h"
#import "AFNetworking.h"
#import "AFHTTPRequestOperation.h"
#import "KDNewfeatureController.h"

#define baseUrl @"http://192.168.1.220/userinfo.do?method=updatePwd"

@interface GBModifyPassWordViewController ()
{
    UIView *topView;
    UILabel *topLabel;
    UIButton *GoBackButton;
    UITextField *userNameField; //用户名输入框
    UITextField *passWordField; //密码输入框
    UITextField *passWordField2; //确认密码输入框
    UIImageView *userNameImage; //用户名图片
    UIImageView *passWordImage; //密码图片
    UIImageView *passWordImage2; //密码图片2
    UIButton *clickInButton; //登录按钮
    NSString *modifyPassWord; //验证修改密码成功与否的接口
}

@end

@implementation GBModifyPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    self.view.backgroundColor = [UIColor colorWithRed:0.8823 green:0.8823 blue:0.8862 alpha:1];
    
    //初始化主界面
    [self createModidfy];
    // Do any additional setup after loading the view.
}

#pragma mark - 创建主界面
-(void)createModidfy
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 31, screenW - 50, 135)];
    view.backgroundColor = [UIColor whiteColor];
    
    //创建输入框
    userNameField = [[UITextField alloc] initWithFrame:CGRectMake(55, 0,screenW - 90, 45)];
    passWordField = [[UITextField alloc] initWithFrame:CGRectMake(55, 45, screenW - 90, 45)];
    passWordField2 = [[UITextField alloc] initWithFrame:CGRectMake(55, 90, screenW - 90, 45)];
    
    userNameField.borderStyle = UITextBorderStyleNone;
    passWordField.borderStyle = UITextBorderStyleNone;
    passWordField2.borderStyle = UITextBorderStyleNone;
    userNameField.placeholder = @"请输入原始密码";
    userNameField.font = [UIFont systemFontOfSize:13];
    userNameField.secureTextEntry = YES;
    passWordField.secureTextEntry = YES;
    passWordField.placeholder = @"请输入新密码";
    passWordField.font = [UIFont systemFontOfSize:13];
    passWordField2.secureTextEntry = YES;
    passWordField2.placeholder = @"确认新密码";
    passWordField2.font = [UIFont systemFontOfSize:13];
    
    userNameField.backgroundColor = [UIColor whiteColor];
    passWordField.backgroundColor = [UIColor whiteColor];
    passWordField2.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 45, screenW - 37, 1)];
    label.backgroundColor = [UIColor lightGrayColor];
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, screenW - 37, 1)];
    label2.backgroundColor = [UIColor lightGrayColor];
    //生成输入框的图片
    userNameImage = [[UIImageView alloc] initWithFrame:CGRectMake(10,0 , 35, 45)];
    userNameImage.image = [UIImage imageNamed:@"登陆页密码"];
    passWordImage = [[UIImageView alloc] initWithFrame:CGRectMake(10,49 , 35, 45)];
    passWordImage.image = [UIImage imageNamed:@"登陆页密码"];
    passWordImage2 = [[UIImageView alloc] initWithFrame:CGRectMake(10,92 , 35, 45)];
    passWordImage2.image = [UIImage imageNamed:@"登陆页密码"];
    
    clickInButton = [UIButton buttonWithType:UIButtonTypeCustom];
    clickInButton.frame = CGRectMake(17,  255 - 64, self.view.frame.size.width - 30, 30);
    [clickInButton setTitle:@"确定" forState:UIControlStateNormal];
    clickInButton.titleLabel.font = [UIFont systemFontOfSize:16];
    clickInButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [clickInButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    clickInButton.backgroundColor = KDColor;
    clickInButton.layer.cornerRadius = 5;
    [clickInButton addTarget:self action:@selector(modify) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *imageClickButton = [[UIImageView alloc] initWithFrame:CGRectMake(17, 255 - 64, screenW - 30, 30)];
    //imageTop.backgroundColor = [UIColor greenColor];
    imageClickButton.image = [UIImage imageNamed:@"12"];
    
    //添加手势
    UITapGestureRecognizer *hide = [[UITapGestureRecognizer alloc] init];
    [hide addTarget:self action:@selector(hideTheKeyBoard)];
    
//    [self.view addSubview:imageClickButton];
    [view addSubview:userNameField];
    [view addSubview:passWordField];
    [view addSubview:passWordField2];
    [view addSubview:userNameImage];
    [view addSubview:passWordImage];
    [view addSubview:passWordImage2];
    [view addSubview:label];
    [view addSubview:label2];
    [self.view addSubview:clickInButton];
    [self.view addGestureRecognizer:hide];
    [self.view addSubview:view];
}

#pragma mark - 隐藏键盘
-(void)hideTheKeyBoard
{
    [self.view endEditing:YES];
}

#pragma mark - 请求服务器检查修改密码的正确性
-(void)modify
{
    if(passWordField.text == nil || passWordField2.text == nil || userNameField.text == nil)
    {
        UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"输入不能为空" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
        [NSTimer scheduledTimerWithTimeInterval:1.50f
                                         target:self
                                       selector:@selector(timerFireMethod:)
                                       userInfo:promptAlert
                                        repeats:YES];
        [promptAlert show];
        return;
    }
    
    if(![passWordField.text isEqualToString:passWordField2.text])
    {
        UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"两次新密码不相同" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
        [NSTimer scheduledTimerWithTimeInterval:1.50f
                                         target:self
                                       selector:@selector(timerFireMethod:)
                                       userInfo:promptAlert
                                        repeats:YES];
        [promptAlert show];
        return;
    }
    
    KDAccount *account = [KDAccountTool account];
    // 通过验证请求服务器
    modifyPassWord = [NSString stringWithFormat:@"%@%@%@%@%@%@%@",baseUrl,@"&token=",account.tokens,@"&old_Pwd=",userNameField.text,@"&new_Pwd=",passWordField.text];
    //NSLog(@"%@",modifyPassWord);
    //请求数据
    NSURLRequest *httpReq = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:modifyPassWord]];
    AFHTTPRequestOperation *reqOper = [[AFHTTPRequestOperation alloc] initWithRequest:httpReq];
    [reqOper setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *op,id success){
        NSHTTPURLResponse *response = [op response];
        
        NSInteger code = [response statusCode];
        if (code == 200)
        {
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[op responseData] options:NSJSONReadingMutableContainers error:nil];
            if([jsonDict[@"status"] isEqualToString:@"ok"])
            {
                UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"修改成功" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
                [NSTimer scheduledTimerWithTimeInterval:1.50f
                                                 target:self
                                               selector:@selector(timerFireMethod:)
                                               userInfo:promptAlert
                                                repeats:YES];
                [promptAlert show];
                
                KDNewfeatureController *login = [[KDNewfeatureController alloc] init];
                //login.URLString = sting111;
                //跳转到登陆界面
                //设置切换风格
                login.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
                //2.切换控制器
                [self presentViewController:login animated:YES completion:nil];
            }
        }
    } failure:^(AFHTTPRequestOperation *op, NSError *error) {
        if (error) {
            //NSLog(@"req error %@",error);
            UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"修改失败" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
            [NSTimer scheduledTimerWithTimeInterval:1.50f
                                             target:self
                                           selector:@selector(timerFireMethod:)
                                           userInfo:promptAlert
                                            repeats:YES];
            [promptAlert show];
        }
    }];
    [reqOper start];
}

#pragma mark - 弹出的对话框内容
- (void)timerFireMethod:(NSTimer*)theTimer//弹出框
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];
    [promptAlert dismissWithClickedButtonIndex:0 animated:NO];
    promptAlert = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end